const ExprotExcel = {
  exprot(path, params, requestType, fileName, token, tenant) {

    const request = new XMLHttpRequest();

    let url = path
      let value = null;
    if (!!params) {
        value = params.slice(1);
        url  = path + '?' + value;
    }

    request.responseType = 'blob';

    request.open(requestType, url, true);
    request.setRequestHeader('X-Okapi-Tenant', tenant);
    request.setRequestHeader('X-Okapi-Token', token);

    request.onload = function (oEvent) {
      if (
        (request.status >= 200 && request.status < 300) ||
        request.status === 304
      ) {

        const content = request.response;
        // 数据转换为文件下载
        const elink = document.createElement('a');
        elink.download = fileName + '.xls';
        elink.style.display = 'none';
        const blob = new Blob([content]);
        elink.href = URL.createObjectURL(blob);
        document.body.appendChild(elink);
        elink.click();
        document.body.removeChild(elink);
        alert('导出成功!文件正在下载...');
      } else {
      }
    };

    request.send(null);
  },
  toQueryPair(key, value) {
    if (typeof value === 'undefined') {
      return `&${key}=`;
    }
    return `&${key}=${value}`;
  },
  objToParam(param) {
    if (Object.prototype.toString.call(param) !== '[object Object]') {
      return '';
    }
    let queryParam = '';
    for (const key in param) {
      if (param.hasOwnProperty(key)) {
        const value = param[key];
        queryParam += this.toQueryPair(key, value);
      }
    }
    return queryParam;
  }
};
export default ExprotExcel;
